package com.bwie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bwie.common.constants.LoginConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domainThree.Contact;
import com.bwie.common.domainThree.Customer;
import com.bwie.common.domainThree.Follow;
import com.bwie.common.domainThree.Type;
import com.bwie.common.domainThree.request.CustomerRequest;
import com.bwie.common.domainThree.request.FollowRequest;
import com.bwie.common.domainThree.response.ContactReqponse;
import com.bwie.common.domainThree.response.CustomerResponse;
import com.bwie.common.result.Result;
import com.bwie.mapper.CustomerMapper;
import com.bwie.service.CustomerService;
import com.bwie.exception.CustomerException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @className: CustomerServiceImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author: 张雨乐
 */
@Service
@Log4j2
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    HttpServletRequest request;
    @Override
    public Result list(CustomerRequest customerRequest) {
        PageHelper.startPage(customerRequest.getPageNum(),customerRequest.getPageSize());
        List<CustomerResponse>responseList=customerMapper.list(customerRequest);
        PageInfo<CustomerResponse> info = new PageInfo<>(responseList);
        return Result.success(info);
    }

    /**
     * 查看这个部门的所有员工
     * @param customerId
     * @return
     */
    @Override
    public Result contactList(String customerId) {
        List<ContactReqponse>list=customerMapper.contactList(customerId);
        return Result.success(list);
    }

    @Override
    public Result typeList(String id) {
        List<Type>list=new ArrayList<>();
        if (!redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + id)){
           list=customerMapper.typeList(id);
            redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY + id,list,60, TimeUnit.MINUTES);
        }else {
            list = (List<Type>) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + id);
        }


        return Result.success(list);
    }

    @Override
    public Result contactAdd(Contact contact) {
        String phone = contact.getPhone();
        String name = contact.getName();
        String role = contact.getRole();
        if (StrUtil.isBlank(phone)){
            throw new CustomerException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        if (StrUtil.isBlank(name)){
            throw new CustomerException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        if (StrUtil.isBlank(role)){
            throw new CustomerException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        //手机号格式错误
        if (!phone.matches("^1[3-9][0-9]{9}$")){
            log.info("手机号格式错误");
            throw new CustomerException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        customerMapper.contactAdd(contact);

        return Result.success("联系人添加成功");
    }

    @Override
    public Result customerAdd(Customer contact) {
        customerMapper.CustomerAdd(contact);
        return Result.success("添加成功");
    }

    @Override
    public Result getContact(String id) {
        Contact contact=customerMapper.getContact(id);
        return Result.success(contact);
    }

    @Override
    public Result followList(String id) {
        List<Follow>list=customerMapper.followList(id);
        return Result.success(list);
    }

    @Override
    public Result followAdd(FollowRequest followRequest) {
        Integer level = followRequest.getLevel();
        Integer typeId = followRequest.getTypeId();
        if (level!=null && typeId!=null){
            customerMapper.customerUp(followRequest);
        }
        customerMapper.followAdd(followRequest);
        return Result.success("添加成功");
    }

    @Transactional
    @Override
    public Result customerDel(List<CustomerResponse> customerResponse) {

        List<Integer> list = new ArrayList<>();
        for (CustomerResponse response : customerResponse) {
            list.add(response.getId());
        }
        if (list==null){
            throw new CustomerException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        //删除跟进记录表
        customerMapper.followDel(list);
        //删除部门的联系人表
        customerMapper.contactDel(list);
        //删除部门
        customerMapper.customerDel(list);
        return Result.success("删除成功");
    }
}
